import React, {useMemo} from "react";
import useAuthorize from "@/hooks/useAuthorize";
import {AuthProps} from "@/components/AuthWrapper/type";

// @ts-ignore
const AuthWrapper: React.FC<AuthProps> = ({authorize, type, children}) => {
  const [, hasAuth] = useAuthorize();

  // @ts-ignore
  const authorized: boolean = useMemo(() => hasAuth(authorize), [authorize, type, hasAuth]);


  if (typeof children === "function") {
    return children(authorized);
  }

  return authorized ? children : null;
};

export default AuthWrapper;
